﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;

namespace Stanza.MSBuild.Tasks
{
    public abstract class FileHandler
    {
        public const string MSBuildNamespace = "http://schemas.microsoft.com/developer/msbuild/2003";

        private PublishInfo pubInfo;

        protected FileHandler( PublishInfo pubInfo )
        {
            this.pubInfo = pubInfo;
        }

        public PublishInfo Info
        {
            get { return pubInfo; }
        }

        public abstract void ParseFile( string fileName );

        protected Stream OpenSourceFile( string fileName )
        {
            return new FileStream( fileName, FileMode.Open, FileAccess.Read, FileShare.Read );
        }

        protected string ReadSourceFileText( string fileName )
        {
            string fileText;
            using( StreamReader textReader = new StreamReader( OpenSourceFile( fileName ) ) )
                fileText = textReader.ReadToEnd();
            return fileText;
        }

        protected XDocument ReadSourceFileXml( Stream readStream )
        {
            XmlReaderSettings rdrSett = new XmlReaderSettings();
            rdrSett.CheckCharacters = true;
            rdrSett.CloseInput = true;
            rdrSett.ConformanceLevel = ConformanceLevel.Document;
            rdrSett.IgnoreComments = false;
            rdrSett.IgnoreProcessingInstructions = false;
            rdrSett.IgnoreWhitespace = false;

            XDocument xmlDoc;
            using( XmlReader xmlRdr = XmlReader.Create( readStream, rdrSett ) )
                xmlDoc = XDocument.Load( xmlRdr );
            return xmlDoc;
        }

        protected XDocument ReadSourceFileXml( string fileName )
        {
            return ReadSourceFileXml( OpenSourceFile( fileName ) );
        }

        protected Stream SaveSourceFileText( string sourceText )
        {
            return SaveSourceFileText( sourceText, new UTF8Encoding( true, true ) );
        }

        protected Stream SaveSourceFileText( string sourceText, Encoding streamEncoding )
        {
            TempStream tempStream = new TempStream();
            using( StreamWriter writer = new StreamWriter( tempStream, streamEncoding ) )
                writer.Write( sourceText );
            return tempStream;
        }

        protected Stream SaveSourceFileXml( XDocument xmlDoc )
        {
            XmlWriterSettings wrtSett = new XmlWriterSettings();
            wrtSett.CloseOutput = true;
            wrtSett.Indent = false;
            wrtSett.OmitXmlDeclaration = true;
            wrtSett.ConformanceLevel = ConformanceLevel.Document;

            TempStream tempStream = new TempStream();
            using( XmlWriter writer = XmlWriter.Create( tempStream, wrtSett ) )
                xmlDoc.Save( writer );
            return tempStream;
        }
    }
}
